package base

import (
	"errors"
	"gitee.com/gopher2011/go-zero-gva/internal/svc"
	"gitee.com/gopher2011/go-zero-gva/internal/types"
	"gitee.com/gopher2011/go-zero-gva/utils"
	uuid "github.com/satori/go.uuid"
	"gorm.io/gorm"
)

func (l *Base) Register(req types.RegisterReq) (*types.Response, error) {
	var (
		authorities []svc.SysAuthority
		user        svc.SysUser
	)
	for _,v := range req.AuthorityIds {
		authorities = append(authorities, svc.SysAuthority{
			AuthorityId: v,
		})
	}
	// 判断用户名是否注册
	if !errors.Is(l.SvcCtx.DB.Where("username = ?",req.Username).First(&user).Error,gorm.ErrRecordNotFound){
		return &types.Response{Code: 7,Msg: "注册失败",Data: svc.SysUserResp{
			User: user,
		} }, errors.New("用户名已注册")
	}

	// 否则 附加uuid 密码md5简单加密 注册
	u := svc.SysUser{
		UUID: uuid.NewV4(),
		Username: req.Username,
		NickName: req.NickName,
		Password: utils.MD5V([]byte(req.Password)),
		HeaderImg: req.HeaderImg,
		AuthorityId: req.AuthorityId,
		Authorities: authorities,
	}
	err := l.SvcCtx.DB.Create(&u).Error
	if err != nil {
		return &types.Response{
			Code: 7,
			Msg: "注册失败",
			Data: svc.SysUserResp{
				User: u,
			},
		}, err
	}
	return &types.Response{Code: 0,Msg: "注册成功",Data: svc.SysUserResp{
		User: u,
	}},nil
}

/* 接口测试参数
Post /user/register
{
   "userName": "admin01",
	"passWord": "root001",
	"nickName": "vip",
	"headerImg": "https://",
	"authorityId":"999",
	"authorityIds":["001","002","003"]
}
*/
/*
{
	"code": 0,
	"data": {
		"user": {
			"ID": 1,
			"CreatedAt": "2021-10-11T19:02:54.808+08:00",
			"UpdatedAt": "2021-10-11T19:02:54.808+08:00",
			"uuid": "c632ef42-e03a-4046-a9f2-b68fbe189ca5",
			"userName": "admin01",
			"nickName": "vip",
			"headerImg": "https://",
			"authority": {
				"CreatedAt": "0001-01-01T00:00:00Z",
				"UpdatedAt": "0001-01-01T00:00:00Z",
				"DeletedAt": null,
				"authorityId": "",
				"authorityName": "",
				"parentId": "",
				"dataAuthorityId": null,
				"children": null,
				"menus": null,
				"defaultRouter": ""
			},
			"authorityId": "999",
			"sideMode": "dark",
			"activeColor": "#1890ff",
			"baseColor": "#fff",
			"authorities": [
				{
					"CreatedAt": "2021-10-11T19:02:54.808+08:00",
					"UpdatedAt": "2021-10-11T19:02:54.808+08:00",
					"DeletedAt": null,
					"authorityId": "001",
					"authorityName": "",
					"parentId": "",
					"dataAuthorityId": null,
					"children": null,
					"menus": null,
					"defaultRouter": "dashboard"
				},
				{
					"CreatedAt": "2021-10-11T19:02:54.808+08:00",
					"UpdatedAt": "2021-10-11T19:02:54.808+08:00",
					"DeletedAt": null,
					"authorityId": "002",
					"authorityName": "",
					"parentId": "",
					"dataAuthorityId": null,
					"children": null,
					"menus": null,
					"defaultRouter": "dashboard"
				},
				{
					"CreatedAt": "2021-10-11T19:02:54.808+08:00",
					"UpdatedAt": "2021-10-11T19:02:54.808+08:00",
					"DeletedAt": null,
					"authorityId": "003",
					"authorityName": "",
					"parentId": "",
					"dataAuthorityId": null,
					"children": null,
					"menus": null,
					"defaultRouter": "dashboard"
				}
			]
		}
	},
	"msg": "注册成功"
}
*/
